package Entities;

import digitshoplogic.DBAccessSingleton;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Pickup number is automatically increased
 * @author Administrator
 */
public class Pickup {
	public int pickUpNo;
	public int storeId;

         public static final Pickup getPickupByID(int id) throws SQLException {
         Pickup t = new Pickup();

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from pickup where pickup.pickUpNo = ?");
         ps.setInt(1, id);

        ResultSet rs = ps.executeQuery();

         while (rs.next()) {
            t.pickUpNo = rs.getInt(1);
            t.storeId = rs.getInt(2);
        }
        rs.close();
        return t;
        }
         /**
          * create a pickup and save it to db
          * @param store_id
          * @return
          * @throws SQLException
          */
     public static final Pickup createPickup(int store_id) throws SQLException {

         Pickup p = new Pickup();
         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO pickup (storeId) VALUES (?)");

         ps.setInt(1, store_id);
         ps.executeUpdate();

         ps = ps = con.prepareStatement("select max(pickUpNo) from pickup");
         ResultSet rs = ps.executeQuery();
         if(rs.next()){
             p.pickUpNo = rs.getInt(1);
             p.storeId = store_id;
         }

         ps.close();
         return p;
        }

 public static final void deletePickupByID(int id) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM pickup WHERE pickup.pickUpNo= ?");
	 ps.setInt(1, id);
         ps.executeUpdate();
         ps.close();
     }
  public static final void updatePickupByObj(Pickup t) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("UPDATE pickup SET "
                 + "pickup.storeId = ? WHERE pickup.pickUpNo = ?");

	 ps.setInt(2, t.pickUpNo);
         ps.setInt(1, t.storeId);

         ps.executeUpdate();
         ps.close();

     }


    public int getStoreId() {
        return storeId;
    }

    public void setStoreId(int storeId) {
        this.storeId = storeId;
    }

    public int getPickUpNo() {
        return pickUpNo;
    }

    public void setPickUpNo(int pickUpNo) {
        this.pickUpNo = pickUpNo;
    }  
}